package com.ruoyi.electricity.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.electricity.domain.ElectricityData;
import com.ruoyi.electricity.domain.TRiskWarnings;
import com.ruoyi.electricity.service.ElectricityDataService;
import com.ruoyi.electricity.service.ElectricityEquipTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/electricity/alarm")
public class ElectricityAlarmController extends BaseController {
    @Autowired
    private ElectricityDataService electricityDataService;
    @Autowired
    private ElectricityEquipTypeService electricityEquipTypeService;

    /**
     * 配置: 报警阈值,设备重要等级,设备类型
     */
    @PutMapping("updateThresholdsForNormalAndAlarmAndWarn")
    public AjaxResult updateThresholdsForNormalAndAlarmAndWarn(@RequestBody ElectricityData electricityData) {
        validateThresholds(electricityData);
        electricityDataService.updateThresholdsForNormalAndAlarmAndWarn(electricityData);

        String equipCode = electricityData.getEquipCode();
        Integer equipTypeId = electricityData.getEquipTypeId();
        electricityEquipTypeService.deleteEquipAndEquipTypeAssociation(equipCode);
        if (equipTypeId != null) {
            electricityEquipTypeService.updateEquipAndEquipTypeAssociation(equipCode, equipTypeId);
        }
        return AjaxResult.success();
    }

    /**
     * 查询报警列表
     */
    @GetMapping("selectElectricityAlarmList")
    public TableDataInfo selectElectricityAlarmList (TRiskWarnings riskWarnings, Date startTime, Date endTime){
        startPage();
        List<TRiskWarnings> list = electricityDataService.selectElectricityAlarmList(riskWarnings, startTime, endTime);
        return getDataTable(list);
    }

    /**
     * 报警处置
     */
    @PutMapping("handleElectricityAlarm")
    public AjaxResult handleElectricityAlarm(@RequestBody TRiskWarnings riskWarnings) {
        if (riskWarnings.getHandleStatus() == null) {
            return error("处置状态不能为空");
        }
        if (riskWarnings.getHandleTime() == null) {
            return error("处置时间不能为空");
        }
        if (riskWarnings.getHandleAction() == null) {
            return error("处置措施不能为空");
        }
        electricityDataService.editElectricityAlarmHandle(riskWarnings);
        return success();
    }
    private void validateThresholds(ElectricityData electricityData) {
        String equipCode = electricityData.getEquipCode();
        Double currentMinWarnVal = electricityData.getCurrentMinWarnVal();
        Double currentMinNormalVal = electricityData.getCurrentMinNormalVal();
        Double currentMaxNormalVal = electricityData.getCurrentMaxNormalVal();
        Double currentMaxWarnVal = electricityData.getCurrentMaxWarnVal();
        Double voltageMinWarnVal = electricityData.getVoltageMinWarnVal();
        Double voltageMinNormalVal = electricityData.getVoltageMinNormalVal();
        Double voltageMaxNormalVal = electricityData.getVoltageMaxNormalVal();
        Double voltageMaxWarnVal = electricityData.getVoltageMaxWarnVal();
        Integer equipTypeId = electricityData.getEquipTypeId();
        if (equipCode == null) {
            throw new IllegalArgumentException("equipCode不能为空");
        }
        if (electricityData.getEquipLevel() == null) {
            throw new IllegalArgumentException("设备的重要等级equipLevel不能为空");
        }
        if (equipTypeId == null) {
            throw new IllegalArgumentException("设备类型不能为空");
        }
        if (currentMinWarnVal == null || currentMinNormalVal == null || voltageMinWarnVal == null || voltageMinNormalVal == null || currentMaxNormalVal == null || currentMaxWarnVal == null || voltageMaxNormalVal == null || voltageMaxWarnVal == null) {
            throw new IllegalArgumentException("阈值不能为空");
        }
        if (!(currentMaxWarnVal > currentMaxNormalVal && currentMaxNormalVal > currentMinNormalVal && currentMinNormalVal> currentMinWarnVal)) {
            throw new IllegalArgumentException("电流的四个阈值必须由小到大");
        }
        if (!(voltageMaxWarnVal > voltageMaxNormalVal && voltageMaxNormalVal > voltageMinNormalVal && voltageMinNormalVal > voltageMinWarnVal)) {
            throw new IllegalArgumentException("电压的四个阈值必须由小到大");
        }

    }

}
